package jlxy.ise.eroad.controller;

import com.google.gson.Gson;
import jlxy.ise.eroad.service.SampleService;
import jlxy.ise.eroad.service.Sret;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 例子，其余的controller照着写
 * @author yecq
 */
@RestController
public class SampleController {
    private static Logger log=Logger.getLogger(SampleController.class);

    @Autowired
    private SampleService us;
    
    // 添加用户
    @RequestMapping("/add")
    public String do_add(@RequestParam("username") String username, @RequestParam("passwd") String passwd) {
        Sret sr = us.add(username, passwd);
        return sret2json(sr);
    }

    // 列出所有用户
    @RequestMapping("/list")
    public String do_list() {
        Sret sr = us.list();
        log.debug("/list, success");
        return sret2json(sr);
    }

    // 删除用户
    @RequestMapping("/remove")
    public String do_remove(@RequestParam("id") String id) {
        Sret sr = us.remove(id);
        return sret2json(sr);
    }
    @RequestMapping("/search")
    public String do_search(@RequestParam("id")String id){
      return us.search(id);
    }
    @RequestMapping("/cc")
    public String do_search1(@RequestParam("username")String username,@RequestParam("passwd")String passwd){
        return us.searchUser(username, passwd);
    }
    @RequestMapping("/exception1")
    public String do_exception1() {
        Sret sr = us.exception1();
        return sret2json(sr);
    }

    private String sret2json(Sret sr) {
        return new Gson().toJson(sr);
    }
}
